{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3263b78a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#coding=utf-8\n",
    "class Person:\n",
    "    @classmethod\n",
    "    def eat(cls):\n",
    "        print(\"类方法eat:\",cls)\n",
    "         \n",
    "    @staticmethod\n",
    "    def sleep(p):\n",
    "        print(\"静态方法sleep：\",p)\n",
    "Person.eat()\n",
    "Person.sleep(\"info\")\n",
    "p = Person()\n",
    "p.eat()\n",
    "p.sleep('info')\n",
    "\n",
    "## 结论:\n",
    "## 不管是用类或者对象调用静态方法，Python都不会对静态方法的第一个参数进行自动绑定\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3912f95d",
   "metadata": {},
   "source": [
    "使用@符号引用已有函数后，可用于修饰其他函数。\n",
    "\n",
    "例如@函数A 装饰 函数B ，实际完成的步骤为：\n",
    "\n",
    "1. 将被装饰的函数B作为参数传给函数A\n",
    "2. 将函数B替换为第1步的返回值。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd47173d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def funA(fn):\n",
    "    \"\"\"参数为一个函数对象\"\"\"\n",
    "    print('A')\n",
    "    fn() #执行传入的fn函数\n",
    "    return 'tizer'\n",
    "    \n",
    "'''\n",
    "下面的代码相当于funA(funB)\n",
    "funB将会被替换为该语句的返回值\n",
    "由于funA返回tizer，因此funB就是tizer\n",
    "'''\n",
    "@funA\n",
    "def funB():\n",
    "    print('B')\n",
    "print(funB)\n",
    "\n",
    "# 既然funB作为参数传给了funA，那就是得先执行funA中的代码，所以执行了print(‘A’)，打印了A，\n",
    "# 然后执行第4行fn(),因为传入的funB，funB中的代码为print('B'),所以打印了B，\n",
    "# 然后funA 返回了一个字符串tizer，所以funB 等同于 tizer，第15行print(funB) 等同于 print('tizer'),所以打印了tizer\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "922d8adb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
